package net.melon.agency.pojo.dto.impl;

import java.util.Date;
import java.util.List;

import javax.inject.Named;
import javax.inject.Singleton;

import net.esj.basic.core.engine.tools.crypt.inst.PwdEncryptor;
import net.esj.basic.core.view.tools.ActionContext;
import net.esj.basic.dao.querybuilder.QueryBuilder;
import net.esj.basic.dao.querybuilder.QueryBuilderFactory;
import net.esj.basic.pojo.system.UserRule;
import net.esj.basic.service.impl.DefaultServiceImpl;
import net.esj.basic.service.provider.UserRuleProvider;
import net.melon.agency.inst.ssh.service.AuthService;
import net.melon.agency.pojo.Agency;
import net.melon.agency.pojo.Agency.AgencyState;
import net.melon.agency.pojo.AgencyUser;
import net.melon.agency.pojo.AgencyUser.AgencyUserState;
import net.melon.agency.pojo.dto.AgencyLogin;
import net.melon.agency.pojo.dto.AgencyMenu;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

@Singleton
@Named("agencyLogin")
public class AgencyLoginImpl extends DefaultServiceImpl implements AgencyLogin {

	@Autowired
	PwdEncryptor pwdEncryptor ;
	
	@Autowired
	AgencyMenu agencyMenu;
	
	@Autowired
	AuthService authService;
	
	@Override
	public boolean alterPwd(String oldPwd, String newPwd, String captcha) {
		if(ActionContext.getSession()==null){
			return false;
		}
		Object o = UserRuleProvider.getSessionUser(ActionContext.getSession(), AgencyUser.class);
		if(!(o instanceof AgencyUser)){
			return false;
		}
		AgencyUser user = (AgencyUser) o;
		String oldAfterEncrypt = oldPwd;
		String newAfterEncryt = newPwd;
		if(pwdEncryptor!=null){
			oldAfterEncrypt = pwdEncryptor.encrypt(oldPwd, user.getName());
			newAfterEncryt = pwdEncryptor.encrypt(newPwd, user.getName());
		}
		if(!StringUtils.equals(oldAfterEncrypt, user.getPassword())){
			return false;
		}
		user.setPassword(newAfterEncryt);
		update(user);
		return true;
	}

	@Override
	public AgencyUser doLoginByNameAndPwd(String name, String pwd,
			String captcha) {
		//System.out.println("SESSIONID:"+ActionContext.getSession().getId());
		QueryBuilder<AgencyUser> qb = QueryBuilderFactory.create( AgencyUser.class);
		
		String pass = pwd;
		if(pwdEncryptor!=null){
			pass = pwdEncryptor.encrypt(pwd, name);
		}
		qb.eq("name", name).eq("password", pass);
		List<AgencyUser> list = queryListByQueryBuilder(qb);
		
		AgencyUser user= null;
		if(!list.isEmpty()){
			user =  list.get(0);
			if(user.getState()!=AgencyUserState.NORMAL.getState()){
				return null;
			}
			if(user.getAgency()!=null){
				Agency agency = get(user.getAgency().getId(), Agency.class);
				if(agency.getState()!=AgencyState.NORMAL.getState()){
					return null;
				}
				user = authService.getUserWithAuth(user.getId());
				user.setLastLoginTime(new Date());
				user.setLastLoginIp(ActionContext.getClient().getIp());
				user.setAgency(agency);
				update(user);
			}else if(user.getAgency()==null){//没有机构的申请号
				user = authService.getUserWithAuth(user.getId());
				user.setLastLoginTime(new Date());
				user.setLastLoginIp(ActionContext.getClient().getIp());
				update(user);
			}else{
				return null;
			}
		}
		if(ActionContext.getSession()!=null && user!=null){
			ActionContext.getSession().setAttribute(getSessionKey(), user);
		}
		agencyMenu.getMenus();
		return user;
	}

	@Override
	public void logout() {
		if(ActionContext.getSession()!=null){
			ActionContext.getSession().removeAttribute(getSessionKey());
			ActionContext.getSession().removeAttribute(getSessionBag());
		}
	}

	@Override
	public Object getUserInfo() {
		return ActionContext.getSession().getAttribute(getSessionKey());
	}

	@Override
	public String getUsername() {
		if(getUserInfo()!=null){
			return ((AgencyUser)getUserInfo()).getName();
		}
		return null;
	}
	
	@Override
	public boolean isAgency(){
		return true;
	}

	protected String getSessionKey(){
		UserRule rule = UserRuleProvider.getUserRule(AgencyUser.class);
		if(rule!=null){
			return rule.getSessionKey();
		}
		return null;
	}
	
	protected String getSessionBag(){
		UserRule rule = UserRuleProvider.getUserRule(AgencyUser.class);
		if(rule!=null){
			return rule.getSessionBag();
		}
		return null;
	}

	@Override
	public Object superLoing(String name, String pwd, String captcha) {
		return null;
	}
}
